package middleware

import (
	"mini-app/internal/dal"
	"mini-app/internal/dao"
	"mini-app/internal/define"

	"192.168.1.75/go-pkg/errorx"
	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
)

// PhoneCheck 手机号校验
func PhoneCheck() gin.HandlerFunc {
	return func(c *gin.Context) {
		openid := c.GetString("openid")
		// 验证登录
		res, err := dao.User{}.UserInfo(c.Request.Context(), dal.Q, openid, "", "")
		if err != nil || res == nil || res.ID == "" {
			response.JSON(c, nil, errorx.New("请重新登录", define.ErrTokenEmpty))
			c.Abort()
			return
		}
		if res.Phone == "" {
			response.JSON(c, nil, errorx.New("请授权手机号", define.ErrPhoneEmpty))
			c.Abort()
			return
		}
		c.Next()
	}
}
